# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=uncrustify
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.72.0
pkgrel=2
pkgdesc="Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="http://uncrustify.sourceforge.net/"
license=(GPL)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/uncrustify/uncrustify/archive/${_realname}-${pkgver}.tar.gz"
        "https://github.com/uncrustify/uncrustify/pull/2866.patch")
sha512sums=('bfcbdf0d8198e1dd91f1c8283441a56669610a84d55a17cdd504571da09b26fef026fd3f753acd9950e498f574e6771a206c72b76ffdea97747b7a4fdb481862'
            '049251d0143ade1e3b2391806d421c0f2c643f1bafb29ee0a8074f028ab391af9cd6cacd2778ff66b8fee3f65503be96b744b702720d712d9b30b5cc45443dde')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"

  # Fix install directories
  patch -Np1 -i "${srcdir}/2866.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "../${_realname}-${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  install -d -m755 "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
  install -m644 etc/*.cfg "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
}
